﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;
using YJKView;

namespace DoorAndWindow
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDoorInsert : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			HideWarningAndErroringKit @object = new HideWarningAndErroringKit(new List<string>
			{
				"DoorInsert"
			}, HideWarningAndErroringKit.HideKitEnum.HideWarning);
			Result result;
			try
			{
				using (DlgWinDoorInsert dlgWinDoorInsert = new DlgWinDoorInsert(cmdData.Application, true))
				{
					try
					{
						for (;;)
						{
							DialogResult dialogResult = dlgWinDoorInsert.ShowDialog();
							if (dialogResult != DialogResult.OK)
							{
								break;
							}
							ImpDoorWinInsert impDoorWinInsert = new ImpDoorWinInsert(this.m_uiApp, dlgWinDoorInsert.m_datum);
							FamilySymbol familySymbol;
							if (dlgWinDoorInsert.rdoFamilyLib.Checked)
							{
								familySymbol = dlgWinDoorInsert.m_datum.m_symbol.Create1(this.m_doc);
								if (dlgWinDoorInsert.m_nInsertType == InsertType.eByEqual || dlgWinDoorInsert.m_nInsertType == InsertType.eByButtress || dlgWinDoorInsert.m_nInsertType == InsertType.eByGridLineEqual)
								{
									int num = familySymbol.Family.get_Parameter(BuiltInParameter.FAMILY_HOSTING_BEHAVIOR).AsInteger();
									if (num != 1)
									{
										MessageBox.Show("当前选中的门不支持此种布置方式。", "盈建科提示");
										continue;
									}
								}
								Transaction transaction = new Transaction(this.m_doc, "DoorInsert");
								transaction.Start();
								cmdData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
								transaction.Commit();
							}
							else
							{
								familySymbol = dlgWinDoorInsert.familySymbolDoc;
							}
							if (dlgWinDoorInsert.AddTag)
							{
								Family family = dlgWinDoorInsert.TagDbFamily.Reload(this.m_doc);
								FamilySymbol tagFamilysymbol = null;
								List<FamilySymbol> symbols = family.GetSymbols();
								if (symbols.Count > 0)
								{
									tagFamilysymbol = symbols[0];
								}
								impDoorWinInsert.AddTag = true;
								impDoorWinInsert.TagFamilysymbol = tagFamilysymbol;
								impDoorWinInsert.SecondTagString = dlgWinDoorInsert.SecondTagString;
							}
							switch (dlgWinDoorInsert.m_nInsertType)
							{
							case InsertType.eByPickPoint:
								impDoorWinInsert.PickPtInsert(BuiltInCategory.OST_Doors, familySymbol, dlgWinDoorInsert.cbxHeight.Text);
								break;
							case InsertType.eByEqual:
								impDoorWinInsert.EqualInsert(BuiltInCategory.OST_Doors, familySymbol, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
								break;
							case InsertType.eByButtress:
								impDoorWinInsert.ButtressInsert(BuiltInCategory.OST_Doors, familySymbol, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
								break;
							case InsertType.eByGridLineEqual:
								impDoorWinInsert.GridLineEqualInsert(BuiltInCategory.OST_Doors, familySymbol, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
								break;
							}
						}
						dlgWinDoorInsert.CloseDocuments();
					}
					catch (Exception)
					{
					}
					result = base.YJKExcute(cmdData, ref message, elements);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
				result = base.YJKExcute(cmdData, ref message, elements);
			}
			return result;
		}
	}
}
